import React from "react";
// @ts-ignore
import AmisRenderer from '../../../components/amisRenderer';
import {serviceStatus} from "../miniProgramManage/enum";
import { routes } from "../../../config/route";
import {useNavigate} from "react-router-dom";
import _ from "lodash";

export default function() {
    const navigate = useNavigate()
    const schema = {
        "type": "page",
        "title": "配置/版本管理",
        "toolbar": [
        ],
        "body": [
            {
                "name": "list",
                "type": "crud",
                "api": {
                    "url": "/admin/dev-weapp-list",
                    "method": "get",
                    "data": {
                        "appid": "${appid}",
                        "offset": "${page|minus:1|times:perPage}",
                        "limit": "${perPage|toInt}"
                    },
                },
                "filter": {
                    "body": [
                        {
                            "type": "group",
                            "body": [
                                {
                                    "type": "input-text",
                                    "name": "appid",
                                    "label": "appid",
                                    "clearable": true,
                                    "placeholder": "通过关键字搜索",
                                    "size": "sm"
                                },
                            ]
                        }
                    ],
                    "actions": [
                        {
                            "type": "submit",
                            "label": "查询"
                        },
                        {
                            "type": "reset",
                            "label": "重置",
                        },
                    ]
                },
                "headerToolbar": [
                    {
                        "label": "",
                        "icon": "fa fa-repeat",
                        "type": "button",
                        "actionType": "reload",
                        "target": "list",
                        "align": "right"
                    },
                    {
                        "type": "statistics",
                        "align": "right"
                    }
                ],
                "alwaysShowPagination":true,
                "columns": [
                    {
                        "name": "appid",
                        "label": "AppID"
                    },
                    {
                        "name": "nickName",
                        "label": "名称"
                    },
                    {
                        "name": "releaseInfo",
                        "label": "发布状态",
                        "type": "tpl",
                        "tpl":"<div><%= data.releaseInfo?'已有上线版本':'尚未发布'%></div>" +
                            "<%if(data.releaseInfo){%><div class='text-xs text-secondary'>" +
                            "<%= data.releaseInfo.releaseVersion%> <%= data.releaseInfo.releaseDesc%><br>" +
                            "<%= date(timeZeroCheck(data.releaseInfo.releaseTime),'YYYY-MM-DD HH\\:mm\\:ss')%></div>" +
                            "<%}%>",
                    },
                    {
                        "name": "serviceStatus",
                        "label": "服务状态",
                        "type": "mapping",
                        "map": {
                            ...serviceStatus,
                            "*":"-",
                        }
                    },
                    {
                        "name": "qrcode",
                        "label": "小程序码",
                        "type": "operation",
                        "buttons": [
                            {
                                "visibleOn": "this.funcInfo.includes(17)",
                                "type": "button",
                                "level": "link",
                                "actionType": "dialog",
                                "label": "获取小程序码",
                                "dialog": {
                                    "title": "获取小程序码",
                                    "body": {
                                        "type": "service",
                                        "api": {
                                            "method": "get",
                                            "url": "admin/qrcode?appid=${appid}",
                                        },
                                        "body": {
                                            "type": "html",
                                            "html": "<img  src='data:image/png;base64,<%= data.releaseQrCode %>' style={{ width: '200px', height: '200px' }} alt=''>"
                                        }
                                    },
                                    "actions": []
                                }
                            }
                        ]
                    },
                    {
                        "fixed": "right",
                        "type": "operation",
                        "label": "操作",
                        "buttons": [
                            {
                                "type": "dropdown-button",
                                "label": "发布前配置",
                                "level": "link",
                                "buttons": [
                                    {
                                        "type": "button",
                                        "level": "link",
                                        "actionType": "dialog",
                                        "label": "设置服务器域名",
                                        "dialog": {
                                            "title": "服务器域名",
                                            "size": "md",
                                            "body": {
                                                "type": "form",
                                                "name": "form1",
                                                "mode": "normal",
                                                "api": {
                                                    "method": "post",
                                                    "url": "/custom/modify-server-domain?appid=${appid}",
                                                    "data": {
                                                        "action": "set",
                                                        "requestdomain": "${requestdomain}",
                                                        "uploaddomain": "${uploaddomain}",
                                                        "downloaddomain": "${downloaddomain}",
                                                        "wsrequestdomain": "${wsrequestdomain}",
                                                        "udpdomain": "${udpdomain}",
                                                        "tcpdomain": "${tcpdomain}",
                                                    }
                                                },

                                                "body":  {
                                                    "type": "service",
                                                    "api": {
                                                        "method": "post",
                                                        "url": "/custom/modify-server-domain?appid=${appid}",
                                                        "data": {
                                                            "action": "get",
                                                        }
                                                    },
                                                    "body": [
                                                        {
                                                            "type": "input-array",
                                                            "name": "requestdomain",
                                                            "label": "requestdomain",
                                                            "items": {
                                                                "type": "input-text",
                                                                "clearable": true
                                                            }
                                                        },
                                                        {
                                                            "type": "input-array",
                                                            "name": "uploaddomain",
                                                            "label": "uploaddomain",
                                                            "items": {
                                                                "type": "input-text",
                                                                "clearable": true
                                                            }
                                                        },
                                                        {
                                                            "type": "input-array",
                                                            "name": "downloaddomain",
                                                            "label": "downloaddomain",
                                                            "items": {
                                                                "type": "input-text",
                                                                "clearable": true
                                                            }
                                                        },
                                                        {
                                                            "type": "input-array",
                                                            "name": "wsrequestdomain",
                                                            "label": "wsrequestdomain",
                                                            "items": {
                                                                "type": "input-text",
                                                                "clearable": true
                                                            }
                                                        },
                                                        {
                                                            "type": "input-array",
                                                            "name": "udpdomain",
                                                            "label": "udpdomain",
                                                            "items": {
                                                                "type": "input-text",
                                                                "clearable": true
                                                            }
                                                        },
                                                        {
                                                            "type": "input-array",
                                                            "name": "tcpdomain",
                                                            "label": "tcpdomain",
                                                            "items": {
                                                                "type": "input-text",
                                                                "clearable": true
                                                            }
                                                        },
                                                    ]
                                                },

                                            }
                                        }
                                    },
                                    {
                                        "type": "button",
                                        "level": "link",
                                        "actionType": "dialog",
                                        "label": "隐私保护指引",
                                        "dialog": {
                                            "title": "设置隐私保护指引",
                                            "size": "md",
                                            "body":{
                                                "type": "form",
                                                "name": "form2",
                                                "mode": "horizontal",
                                                "api": {
                                                    "method": "post",
                                                    "url": "/custom/setPrivacySetting?appid=${appid}",
                                                    "data": {
                                                        "privacy_ver": 2,
                                                        "setting_list": "${setting_list}",
                                                        "owner_setting": "${owner_setting}",
                                                    }
                                                },
                                                "body": [
                                                    {
                                                        "type": "service",
                                                        "name": "service2",
                                                        "data": {
                                                            privacy_desc_list: []
                                                        },
                                                        "api": {
                                                            "method": "post",
                                                            "url": "/custom/getPrivacySetting?appid=${appid}",
                                                            "data": {
                                                                "privacy_ver": 2
                                                            },
                                                            adaptor(payload:any) {
                                                                payload.data.privacy_desc_map = {
                                                                    ..._.mapValues(_.keyBy(payload.data.privacy_desc.privacy_desc_list, 'privacy_key'), 'privacy_desc'),
                                                                    "*": "-",
                                                                }
                                                                return payload;
                                                            }
                                                        },
                                                        "body": [
                                                            {
                                                                "type": "fieldSet",
                                                                "title": "要收集的用户信息",
                                                                "mode": "horizontal",
                                                                "body": [
                                                                    {
                                                                        "type": "combo",
                                                                        "name": "setting_list",
                                                                        "label": false,
                                                                        "addable": false,
                                                                        "removable": false,
                                                                        "multiple": true,
                                                                        "canAccessSuperData": true,
                                                                        "strictMode": false,
                                                                        "syncFields": [
                                                                            "privacy_desc_map"
                                                                        ],
                                                                        "items": [
                                                                            {
                                                                                "type": "group",
                                                                                "body": [
                                                                                    {
                                                                                        "type": "tpl",
                                                                                        "tpl": "<%= data.privacy_desc_map[data.privacy_key]||data.privacy_key %>",
                                                                                    },
                                                                                ]
                                                                            },
                                                                            {
                                                                                "name": "privacy_text",
                                                                                "label": false,
                                                                                "type": "input-text",
                                                                                "placeholder": "描述",
                                                                                "required": true,
                                                                            },
                                                                        ]
                                                                    },
                                                                ]
                                                            },
                                                            {
                                                                "type": "fieldSet",
                                                                "title": "联系方式（4选一）",
                                                                "mode": "horizontal",
                                                                "body": [
                                                                    {
                                                                        "name": "owner_setting.contact_email",
                                                                        "label": "邮箱",
                                                                        "type": "input-text",
                                                                        "requiredOn": "!this.owner_setting.contact_email&&!this.owner_setting.contact_phone&&!this.owner_setting.contact_qq&&!this.owner_setting.contact_weixin",
                                                                    },
                                                                    {
                                                                        "name": "owner_setting.contact_phone",
                                                                        "label": "手机",
                                                                        "type": "input-text",
                                                                        "requiredOn": "!this.owner_setting.contact_email&&!this.owner_setting.contact_phone&&!this.owner_setting.contact_qq&&!this.owner_setting.contact_weixin",
                                                                    },
                                                                    {
                                                                        "name": "owner_setting.contact_qq",
                                                                        "label": "qq号",
                                                                        "type": "input-text",
                                                                        "requiredOn": "!this.owner_setting.contact_email&&!this.owner_setting.contact_phone&&!this.owner_setting.contact_qq&&!this.owner_setting.contact_weixin",
                                                                    },
                                                                    {
                                                                        "name": "owner_setting.contact_weixin",
                                                                        "label": "微信号",
                                                                        "type": "input-text",
                                                                        "requiredOn": "!this.owner_setting.contact_email&&!this.owner_setting.contact_phone&&!this.owner_setting.contact_qq&&!this.owner_setting.contact_weixin",
                                                                    },
                                                                ]
                                                            },
                                                            {
                                                                "type": "fieldSet",
                                                                "title": "其他信息",
                                                                "mode": "horizontal",
                                                                "body": [
                                                                    {
                                                                        "name": "owner_setting.notice_method",
                                                                        "label": "通知方式",
                                                                        "type": "input-text",
                                                                        "required": true,
                                                                    },
                                                                ]
                                                            }


                                                        ]
                                                    }
                                                ]
                                            },
                                        }
                                    }
                                ]
                            },
                            {
                                "label": "版本管理",
                                "type": "button",
                                "level": "link",
                                "actionType": "none",
                                onClick: (e: any, scope: any)=>{
                                    const {appid}  =scope.data

                                    navigate(`${routes.miniProgramVersion.path}?appId=${appid}`)
                                }

                            },
                        ]
                    }
                ]
            }
        ]
    };
    return <AmisRenderer schema={schema} />;
}

